//
//  SearchViewController.m
//  MyProject
//
//  Created by dllo on 15/11/4.
//  Copyright (c) 2015年 蓝鸥0825. All rights reserved.
//


//      收索页面
#import "SearchViewController.h"
#import "SearchModel.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface SearchViewController ()<UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate>

@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableViewSearch = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    [self.view addSubview:self.tableViewSearch];
    [self.tableViewSearch release];
    
    self.tableViewSearch.dataSource = self;
    self.tableViewSearch.delegate = self;
    self.tableViewSearch.rowHeight = 150;
    
    
    self.search = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, WIDTH / 100, HEIGHT / 100 * 10)];
    self.tableViewSearch.tableHeaderView = self.search;
    [self.search release];
    [self.search becomeFirstResponder];
    
    [self.search setPlaceholder:@"请输入要收索的内容"];
    [self.search setBarStyle:1];
    [self.search setTranslucent:YES];
    [self.search setShowsSearchResultsButton:YES];
    [self.search setShowsCancelButton:YES animated:YES];
    [self.search setKeyboardType:UIKeyboardTypeDefault];
    [self.search sizeToFit];
    self.search.delegate = self;

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.tabelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *reuse = @"reuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
    }
    cell.textLabel.text = self.tabelArray[indexPath.row][@"title"];
    
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:cell.textLabel.text];
    NSRange range = [cell.textLabel.text rangeOfString:self.search.text];
    [string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
    cell.textLabel.attributedText = string;

    return cell;
    
}


- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    
    NSString *str = [searchText stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];
    [manger GET:[NSString stringWithFormat:@"http://api.meishi.cc/v5/meishiquan_search.php?format=jsonsource=android&page=1&format=json&keyword=%@&", str]parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = responseObject;
        if (dic.count == 0) {
            NSLog(@"无搜索内容");
        }else {
            
            self.tabelArray = dic[@"search_list"];
            
            [self.tableViewSearch reloadData];

        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@", error);
    }];

    
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
    
    [searchBar resignFirstResponder];
    
}









@end













